package es.udc.fic.hamNeggs.manager;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import es.udc.fic.hamNeggs.pojo.Meeting;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

@Repository(value = "meeting")
public class MeetingManager extends AbstractManager {
    private final static Logger LOG = Logger.getLogger(MeetingManager.class.getName());

    @Transactional(readOnly = true)
    @SuppressWarnings("unchecked")
    public List<Meeting> getMetingList() {
        List<Meeting> list = session.createQuery("from Meeting").list();
        Collections.sort(list,
                new Comparator<Meeting>() {
                    public int compare(Meeting o1, Meeting o2) {
                        return o1.getId().intValue() - o2.getId().intValue();
                    }
                });
        return list;
    }

    @Transactional(readOnly = true)
    public Meeting getMeeting(Long id) {
        Meeting mtg = (Meeting) session.get(Meeting.class, id);
        return mtg;
    }

    @Transactional(readOnly = false)
    public void saveMeeting(Meeting meeting) {
        super.save(meeting, LOG);
    }

    @Transactional(readOnly = false)
    public void deleteMeeting(Meeting meeting) {
        super.delete(meeting, LOG);
    }

    @Transactional(readOnly = false)
    public void updateMeeting(Meeting meeting) {
        super.update(meeting, LOG);
    }

}
